package cn.csxw.openchina.ui.fragment.CommentFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import javax.inject.Inject;

import cn.csxw.openchina.base.BaseRecAdapter;
import cn.csxw.openchina.base.BaseRefreshFragment;
import cn.csxw.openchina.constant.Constant;
import cn.csxw.openchina.dagger2.component.DaggerMoveCommentComponent;
import cn.csxw.openchina.dagger2.module.MoveCommentModule;
import cn.csxw.openchina.dagger2.presenter.MoveCommentPresenter;
import cn.csxw.openchina.entity.bean.TweetBean;
import cn.csxw.openchina.ui.adapter.MoveCommentAdapter;
import cn.csxw.openchina.utils.Global;

/**
 * 评论的Fragment
 */

public class MoveCommentFragment extends BaseRefreshFragment {

    public MoveCommentAdapter mAdapter;
    @Inject
    protected MoveCommentPresenter mMoveCommentPresenter;
    private TweetBean mTweetBean;
    public String mId;
    public int mCommentCount;
    private TweetBean mBean;
    private int pageIndex = 0;

    public MoveCommentFragment(){

    }

    @Override
    protected BaseRecAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public void doRefresh() {
        mSrfLayout.setRefreshing(true);
        mMoveCommentPresenter.setIsRefresh();
        loadDate(0);   //  设加载数据及更新界面状态
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new MoveCommentAdapter(getActivity());
        mAdapter.setHasHeader();        //  设置为有头部的类型

        Bundle bundle = getArguments();
        int position = bundle.getInt(Constant.COMMENT_POSITION, 1);
        mBean = (TweetBean) bundle.getSerializable(Constant.TWEETBEAN_INFO);
        mId = mBean.id;
        mAdapter.setOnComment(new BaseRecAdapter.OnComment() {
            @Override
            public int setComment(int position) {
                return mBean.commentCount;
            }

            @Override
            public void setClickLike(View v) {
                Toast.makeText(getActivity(),"点赞",Toast.LENGTH_SHORT).show();
            }

            @Override
            public String setCommentContent(int position) {
                return mBean.body;
            }

            @Override
            public String setLikeName(int position) {
                return null;
            }

            @Override
            public void setHeaderPhoto(ImageView mHeaderPhoto, int position) {
                Glide.with(getActivity()).load(mBean.portrait).into(mHeaderPhoto);
            }

            @Override
            public String setName(int position) {
                return mBean.author;
            }

            @Override
            public String setTime(int position) {
                return mBean.pubDate;
            }
        });

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mMoveCommentPresenter == null) {
            DaggerMoveCommentComponent.builder()
                    .moveCommentModule(new MoveCommentModule(this))
                    .build()
                    .in(this);
            loadDate(0);
        }
        doRefresh();
        setLoadMore();   //  设置加载更多
    }

    /**
     * 设置加载更多
     */
    private void setLoadMore() {
        setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onUpLoadMore() {
                if (Global.isNetworkReachable(Global.mContext)){
                    pageIndex += 1;
                    loadDate(pageIndex);   //加载数据及更新界面状态
                }else {
                    Toast.makeText(getActivity(), "网络连接异常,无法加载更多",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mAdapter.setHasHeader();        //  设置为取消头部的类型
        //mAdapter.isHeader = false;
    }

    /**
     * 加载数据及更新界面状态
     * @param pageIndex
     */
    private void loadDate(int pageIndex) {
        //TODO: CallNetWork中记得根据常数类型个数传Object对象 如:
        mMoveCommentPresenter.callNetWork(pageIndex,3,20,mId);
        Global.onNetworkStateListener(mMoveCommentPresenter,this);
    }
}
